<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="寝室号" prop="dormName">
        <el-input
          v-model="queryParams.dormName"
          placeholder="请输入寝室号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:dorm:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:dorm:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:dorm:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:dorm:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dormList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="寝室id" align="center" prop="dormId" />
      <el-table-column label="寝室号" align="center" prop="dormName" />
      <el-table-column label="所属楼层" align="center" prop="floorId" :formatter="(row) => formaterFloorName(row.floorId)" />
      <el-table-column label="床位排数" align="center" prop="bedRows" />
      <el-table-column label="床位列数" align="center" prop="bedColumns" />
      <el-table-column label="床位层数" align="center" prop="bedFloors" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:dorm:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:dorm:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改寝室信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="dormRef" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="寝室号" prop="dormName">
              <el-input v-model="form.dormName" placeholder="请输入寝室号" />
            </el-form-item>
          </el-col>
          <el-divider />
          <el-col :span="24">
            <el-form-item label="位置" prop="xIndex">
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="楼层" prop="floorId">
              <el-select v-model="form.floorId" placeholder="请选择">
                <el-option v-for="floor in floors" :key="floor.floorId" :label="floor.floorName" :value="floor.floorId"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="左右" prop="xIndex">
              <el-select v-model="form.xIndex" >
                <el-option :key="0" label="左" :value="0" />
                <el-option :key="0" label="右" :value="1" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="序数" prop="yIndex">
              <el-input-number v-model="form.yIndex" controls-position="right" :min="0" />
            </el-form-item>
          </el-col>
          <el-divider />
          <el-col :span="24">
            <el-form-item label="床位" prop="xIndex">
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="排数" prop="bedRows">
              <el-input-number v-model="form.bedRows" controls-position="right" :min="1" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="列数" prop="bedColumns">
              <el-input-number v-model="form.bedColumns" controls-position="right" :min="1" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="层数" prop="bedFloors">
              <el-select v-model="form.bedFloors" placeholder="请选择">
                <el-option key="1" label="单层" value="1"></el-option>
                <el-option key="2" label="上下铺" value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-divider />
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Dorm">
import { listDorm, getDorm, delDorm, addDorm, updateDorm } from "@/api/system/dorm";
import {listFloor} from "@/api/system/floor.js";

const { proxy } = getCurrentInstance();

const dormList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const floors = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    dormName: null,
    floorId: null,
  },
  rules: {
    dormName: [
      { required: true, message: "寝室号不能为空", trigger: "blur" }
    ],
    floorId: [
      { required: true, message: "所属楼层不能为空", trigger: "change" }
    ],
    bedRows: [
      { required: true, message: "床位排数不能为空", trigger: "blur" }
    ],
    bedColumns: [
      { required: true, message: "床位列数不能为空", trigger: "blur" }
    ],
    bedFloors: [
      { required: true, message: "床位层数不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询寝室信息列表 */
function getList() {
  loading.value = true;
  listDorm(queryParams.value).then(response => {
    dormList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
  listFloor(queryParams.value).then(response => {
    floors.value = response.rows;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    dormId: null,
    dormName: null,
    floorId: null,
    bedRows: 2,
    bedColumns: 2,
    bedFloors: 2,
    xIndex: 0,
    yIndex: 0,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  };
  proxy.resetForm("dormRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.dormId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加寝室信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _dormId = row.dormId || ids.value
  getDorm(_dormId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改寝室信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["dormRef"].validate(valid => {
    if (valid) {
      if (form.value.dormId != null) {
        updateDorm(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addDorm(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _dormIds = row.dormId || ids.value;
  proxy.$modal.confirm('是否确认删除寝室信息编号为"' + _dormIds + '"的数据项？').then(function() {
    return delDorm(_dormIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/dorm/export', {
    ...queryParams.value
  }, `dorm_${new Date().getTime()}.xlsx`)
}

function formaterFloorName(id) {
  const target = floors.value.find(item => item.floorId === id)
  return target ? `${target.floorName}（ID: ${id}）` : '数据异常'
}

getList();
</script>
